package com.billdowney.dao.user;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.billdowney.dao.base.BaseJpaDao;
import com.billdowney.entity.base.EntityStatus;
import com.billdowney.entity.user.UserLoginInfo;

@Repository
public interface UserLoginInfoDao extends BaseJpaDao<UserLoginInfo, String> {

	@Query("FROM UserLoginInfo u WHERE(phone = :phone OR email = :email OR username = :username)")
	public List<UserLoginInfo> findList(String phone, String email, String username);

	@Query("FROM UserLoginInfo u WHERE (password=:password OR encryptPassword=:password) AND (phone=:username OR email=:username OR username=:username)")
	public UserLoginInfo findUserLoginInfo(String username, String password);

	/**
	 * 批量更新用户状态
	 * 
	 * @param status 用户状态
	 * @param ids    用户ID
	 * @return 成功条数
	 * @author: 唐路钧
	 * @date: 2019年1月8日 下午6:30:05
	 */
	@Modifying
	@Query("UPDATE UserLoginInfo SET status=:status,updateDate=:updateDate WHERE id IN(:ids)")
	public int updateStatus(EntityStatus status, Date updateDate, @Param(value = "ids") List<String> ids);

	/**
	 * 分页查询用户列表
	 * 
	 * @param pageable 分页参数
	 * @param keyword  关键字
	 * @return
	 * @author: 唐路钧
	 * @date: 2019年1月8日 下午6:41:55
	 */
	@Query("SELECT uli FROM UserLoginInfo uli LEFT JOIN UserInfo ubi ON ubi.id=uli.userInfo where ubi.id = uli.userInfo "
			+ "and (uli.status<>:status or :status is null)"
			+ " AND (uli.username LIKE :keyword OR uli.phone LIKE :keyword OR uli.email LIKE :keyword OR ubi.nickname LIKE :keyword)")
	public Page<UserLoginInfo> findAll(Pageable pageable, EntityStatus status, String keyword);

}
